package com.example.demo.Controller;

import com.example.demo.Service.Userservice;
import com.example.demo.module.To_base64;
import com.example.demo.module.User;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Random;

@RestController
@RequestMapping("/user")
public class Usercontroller {
    @Resource
    private Userservice service;

//    @ResponseBody
    @RequestMapping("/getform")
    public String account_login(String username, String stupwd) {
        String restr = "";
        User u = new User();
        u.setStunum(username);
        u.setStupwd(stupwd);
        //System.out.println(stupwd);
        if (service.form_login(u).equals("用户不存在")) {
            restr = "alert('用户不存在');";
        } else if (service.form_login(u).equals("成功")) {
//            restr = "location.href='/index'";
            //restr=username;
            restr="that.$router.push('/home')";  //eval无法识别this，前端let that=this
//            restr = "location.href='/register'";
        } else {
            restr = "alert('密码错误');";
        }
        return restr;
    }

//    @ResponseBody
    @RequestMapping("/register")
    public String adduser(String username, String stupwd) {
        User u = new User();
        u.setStunum(username);
        u.setStupwd(stupwd);
        if (service.add_user(u).equals("成功")) {
            return "Sucess";
        }
        else if (service.add_user(u).equals("用户已存在")){
            return "alert('用户名已存在');";
        }
        else{
            return "alert('注册失败');";
        }
    }

    @RequestMapping("/getscore")
    public HashMap<Integer,Integer> getscore(String username){
        HashMap<Integer,Integer> hashMap=new HashMap<Integer, Integer>();
        User u = new User();
        u.setStunum(username);
        hashMap=service.getscore(u);
        return hashMap;
    }

    @RequestMapping("/updatesnake")
    public void updatesnake(String username,int snake){
        //System.out.println(username);
        User u=new User();
        u.setStunum(username);
        u.setSnake(snake);
        service.updatesnake(u);
    }

    @RequestMapping("/updateball")
    public void updateball(String username,int ball){
        //System.out.println(username);
        User u=new User();
        u.setStunum(username);
        u.setTantanqiu(ball);
        service.updateball(u);
    }

    @RequestMapping("/updateflybird")
    public void updateflybird(String username,int flybird){
        User u=new User();
        u.setStunum(username);
        u.setFlybird(flybird);
        service.updateflybird(u);
    }

    @RequestMapping("/updateplanewar")
    public void updateplanewar(String username,int planewar){
        //System.out.println(username);
        User u=new User();
        u.setStunum(username);
        u.setPlanewar(planewar);
        service.updateplanewar(u);
    }

    @RequestMapping("/updateParkour")
    public void updataupdataParkour(String username,int Parkour){
        User u=new User();
        u.setStunum(username);
        u.setParkour(Parkour);
        service.updataparkour(u);
    }

    //   @ResponseBody
    @RequestMapping("/test")
    public String test(){
        return "hello";
    }

}
